package org.boyan.modules.cgform.c;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import org.boyan.common.api.vo.Result;
import org.boyan.common.constant.enums.CgformEnum;
import org.boyan.common.system.query.QueryGenerator;
import org.boyan.common.system.util.JwtUtil;
import org.boyan.common.util.oConvertUtils;
import org.boyan.modules.cgform.entity.*;
import org.boyan.modules.cgform.service.IOnlCgformEnhanceService;
import org.boyan.modules.cgform.service.IOnlCgformFieldService;
import org.boyan.modules.cgform.service.IOnlCgformHeadService;
import org.boyan.modules.config.exception.DBException;
import org.jeecgframework.codegenerate.database.DbReadTableUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController("onlCgformHeadController")
@RequestMapping({"/online/cgform/head"})
public class d {
    private static final Logger a = LoggerFactory.getLogger(d.class);

    @Autowired
    private IOnlCgformHeadService onlCgformHeadService;

    @Autowired
    private IOnlCgformFieldService onlCgformFieldService;

    @Autowired
    private IOnlCgformEnhanceService onlCgformEnhanceService;

    private static List<String> b = null;

    @Autowired
    ResourceLoader resourceLoader;

    private static String c;

    @GetMapping({"/list"})
    public Result<IPage<OnlCgformHead>> a(OnlCgformHead paramOnlCgformHead, @RequestParam(name = "pageNo", defaultValue = "1") Integer paramInteger1, @RequestParam(name = "pageSize", defaultValue = "10") Integer paramInteger2, HttpServletRequest paramHttpServletRequest) {
        Result<IPage<OnlCgformHead>> result = new Result<>();
        QueryWrapper<OnlCgformHead> queryWrapper = QueryGenerator.initQueryWrapper(paramOnlCgformHead, paramHttpServletRequest.getParameterMap());
        IPage<OnlCgformHead> page = new Page<>(paramInteger1, paramInteger2);
        IPage<OnlCgformHead> iPage = this.onlCgformHeadService.page(page, queryWrapper);
        if (paramOnlCgformHead.getCopyType() != null && paramOnlCgformHead.getCopyType() == 0)
            this.onlCgformHeadService.initCopyState(iPage.getRecords());
        result.setSuccess(true);
        result.setResult(iPage);
        return result;
    }

    @PostMapping({"/add"})
    public Result<OnlCgformHead> a(@RequestBody OnlCgformHead paramOnlCgformHead) {
        Result<OnlCgformHead> result = new Result<>();
        try {
            this.onlCgformHeadService.save(paramOnlCgformHead);
            result.success("添加成功！");
        } catch (Exception exception) {
            a.error(exception.getMessage(), exception);
            result.error500("操作失败");
        }
        return result;
    }

    @PutMapping({"/edit"})
    public Result<OnlCgformHead> b(@RequestBody OnlCgformHead paramOnlCgformHead) {
        Result<OnlCgformHead> result = new Result<>();
        OnlCgformHead onlCgformHead = (OnlCgformHead)this.onlCgformHeadService.getById(paramOnlCgformHead.getId());
        if (onlCgformHead == null) {
            result.error500("未找到对应实体");
        } else {
            boolean bool = this.onlCgformHeadService.updateById(paramOnlCgformHead);
            if (bool)
                result.success("修改成功!");
        }
        return result;
    }

    @DeleteMapping({"/delete"})
    public Result<?> a(@RequestParam(name = "id", required = true) String paramString) {
        try {
            this.onlCgformHeadService.deleteRecordAndTable(paramString);
        } catch (DBException | SQLException dBException) {
            return Result.error("删除失败" + dBException.getMessage());
        }
        return Result.ok("删除成功!");
    }

    @DeleteMapping({"/removeRecord"})
    public Result<?> b(@RequestParam(name = "id", required = true) String paramString) {
        try {
            this.onlCgformHeadService.deleteRecord(paramString);
        } catch (DBException | SQLException dBException) {
            return Result.error("移除失败" + dBException.getMessage());
        }
        return Result.ok("移除成功!");
    }

    @DeleteMapping({"/deleteBatch"})
    public Result<OnlCgformHead> a(@RequestParam(name = "ids", required = true) String paramString1, @RequestParam(name = "flag") String paramString2) {
        Result<OnlCgformHead> result = new Result<>();
        if (paramString1 == null || "".equals(paramString1.trim())) {
            result.error500("参数不识别！");
        } else {
            this.onlCgformHeadService.deleteBatch(paramString1, paramString2);
            result.success("删除成功!");
        }
        return result;
    }

    @GetMapping({"/queryById"})
    public Result<OnlCgformHead> c(@RequestParam(name = "id", required = true) String paramString) {
        Result<OnlCgformHead> result = new Result<>();
        OnlCgformHead onlCgformHead = this.onlCgformHeadService.getById(paramString);
        if (onlCgformHead == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(onlCgformHead);
            result.setSuccess(true);
        }
        return result;
    }

    @GetMapping({"/queryByTableNames"})
    public Result<?> d(@RequestParam(name = "tableNames", required = true) String paramString) {
        LambdaQueryWrapper<OnlCgformHead> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        String[] arrayOfString = paramString.split(",");
        lambdaQueryWrapper.in(OnlCgformHead::getTableName, Arrays.asList(arrayOfString));
        List<OnlCgformHead> list = this.onlCgformHeadService.list(lambdaQueryWrapper);
        if (list == null)
            return Result.error("未找到对应实体");
        return Result.ok(list);
    }

    @PostMapping({"/enhanceJs/{code}"})
    @CacheEvict(value = {"sys:cache:online:list", "sys:cache:online:form"}, allEntries = true, beforeInvocation = true)
    public Result<?> a(@PathVariable("code") String paramString, @RequestBody OnlCgformEnhanceJs paramOnlCgformEnhanceJs) {
        try {
            paramOnlCgformEnhanceJs.setCgformHeadId(paramString);
            this.onlCgformHeadService.saveEnhance(paramOnlCgformEnhanceJs);
            return Result.ok("保存成功!");
        } catch (Exception exception) {
            a.error(exception.getMessage(), exception);
            return Result.error("保存失败!");
        }
    }

    @GetMapping({"/enhanceJs/{code}"})
    public Result<?> a(@PathVariable("code") String paramString, HttpServletRequest paramHttpServletRequest) {
        try {
            String str = paramHttpServletRequest.getParameter("type");
            OnlCgformEnhanceJs onlCgformEnhanceJs = this.onlCgformHeadService.queryEnhance(paramString, str);
            if (onlCgformEnhanceJs == null)
                return Result.error("查询为空");
            return Result.ok(onlCgformEnhanceJs);
        } catch (Exception exception) {
            a.error(exception.getMessage(), exception);
            return Result.error("查询失败!");
        }
    }

    @PutMapping({"/enhanceJs/{code}"})
    @CacheEvict(value = {"sys:cache:online:list", "sys:cache:online:form"}, allEntries = true, beforeInvocation = true)
    public Result<?> b(@PathVariable("code") String paramString, @RequestBody OnlCgformEnhanceJs paramOnlCgformEnhanceJs) {
        try {
            paramOnlCgformEnhanceJs.setCgformHeadId(paramString);
            this.onlCgformHeadService.editEnhance(paramOnlCgformEnhanceJs);
            return Result.ok("保存成功!");
        } catch (Exception exception) {
            a.error(exception.getMessage(), exception);
            return Result.error("保存失败!");
        }
    }

    @GetMapping({"/enhanceButton/{formId}"})
    public Result<?> b(@PathVariable("formId") String paramString, HttpServletRequest paramHttpServletRequest) {
        try {
            List<OnlCgformButton> list = this.onlCgformHeadService.queryButtonList(paramString);
            if (list == null || list.size() == 0)
                return Result.error("查询为空");
            return Result.ok(list);
        } catch (Exception exception) {
            a.error(exception.getMessage(), exception);
            return Result.error("查询失败!");
        }
    }

    @GetMapping({"/enhanceSql/{formId}"})
    public Result<?> c(@PathVariable("formId") String paramString, HttpServletRequest paramHttpServletRequest) {
        List<OnlCgformEnhanceSql> list = this.onlCgformEnhanceService.queryEnhanceSqlList(paramString);
        return Result.OK(list);
    }

    @PostMapping({"/enhanceSql/{formId}"})
    @CacheEvict(value = {"sys:cache:online:list", "sys:cache:online:form"}, allEntries = true, beforeInvocation = true)
    public Result<?> a(@PathVariable("formId") String paramString, @RequestBody OnlCgformEnhanceSql paramOnlCgformEnhanceSql) {
        try {
            paramOnlCgformEnhanceSql.setCgformHeadId(paramString);
            if (this.onlCgformEnhanceService.checkOnlyEnhance(paramOnlCgformEnhanceSql)) {
                this.onlCgformEnhanceService.saveEnhanceSql(paramOnlCgformEnhanceSql);
                return Result.ok("保存成功!");
            }
            return Result.error("保存失败,该按钮已存在增强配置!");
        } catch (Exception exception) {
            a.error(exception.getMessage(), exception);
            return Result.error("保存失败!");
        }
    }

    @PutMapping({"/enhanceSql/{formId}"})
    @CacheEvict(value = {"sys:cache:online:list", "sys:cache:online:form"}, allEntries = true, beforeInvocation = true)
    public Result<?> b(@PathVariable("formId") String paramString, @RequestBody OnlCgformEnhanceSql paramOnlCgformEnhanceSql) {
        try {
            paramOnlCgformEnhanceSql.setCgformHeadId(paramString);
            if (this.onlCgformEnhanceService.checkOnlyEnhance(paramOnlCgformEnhanceSql)) {
                this.onlCgformEnhanceService.updateEnhanceSql(paramOnlCgformEnhanceSql);
                return Result.ok("保存成功!");
            }
            return Result.error("保存失败,该按钮已存在增强配置!");
        } catch (Exception exception) {
            a.error(exception.getMessage(), exception);
            return Result.error("保存失败!");
        }
    }

    @DeleteMapping({"/enhanceSql"})
    @CacheEvict(value = {"sys:cache:online:list", "sys:cache:online:form"}, allEntries = true, beforeInvocation = true)
    public Result<?> e(@RequestParam(name = "id", required = true) String paramString) {
        try {
            this.onlCgformEnhanceService.deleteEnhanceSql(paramString);
            return Result.ok("删除成功");
        } catch (Exception exception) {
            a.error(exception.getMessage(), exception);
            return Result.error("删除失败!");
        }
    }

    @DeleteMapping({"/deletebatchEnhanceSql"})
    @CacheEvict(value = {"sys:cache:online:list", "sys:cache:online:form"}, allEntries = true, beforeInvocation = true)
    public Result<?> f(@RequestParam(name = "ids", required = true) String paramString) {
        try {
            List<String> list = Arrays.asList(paramString.split(","));
            this.onlCgformEnhanceService.deleteBatchEnhanceSql(list);
            return Result.ok("删除成功");
        } catch (Exception exception) {
            a.error(exception.getMessage(), exception);
            return Result.error("删除失败!");
        }
    }

    @GetMapping({"/enhanceJava/{formId}"})
    public Result<?> a(@PathVariable("formId") String paramString, OnlCgformEnhanceJava paramOnlCgformEnhanceJava) {
        List list = this.onlCgformEnhanceService.queryEnhanceJavaList(paramString);
        return Result.OK(list);
    }

    @PostMapping({"/enhanceJava/{formId}"})
    @CacheEvict(value = {"sys:cache:online:list", "sys:cache:online:form"}, allEntries = true, beforeInvocation = true)
    public Result<?> b(@PathVariable("formId") String paramString, @RequestBody OnlCgformEnhanceJava paramOnlCgformEnhanceJava) {
        try {
            if (!org.boyan.modules.cgform.util.b.a(paramOnlCgformEnhanceJava))
                return Result.error("类实例化失败，请检查!");
            paramOnlCgformEnhanceJava.setCgformHeadId(paramString);
            if (this.onlCgformEnhanceService.checkOnlyEnhance(paramOnlCgformEnhanceJava)) {
                this.onlCgformEnhanceService.saveEnhanceJava(paramOnlCgformEnhanceJava);
                return Result.ok("保存成功!");
            }
            return Result.error("保存失败,配置重复了!");
        } catch (Exception exception) {
            a.error(exception.getMessage(), exception);
            return Result.error("保存失败!");
        }
    }

    @PutMapping({"/enhanceJava/{formId}"})
    @CacheEvict(value = {"sys:cache:online:list", "sys:cache:online:form"}, allEntries = true, beforeInvocation = true)
    public Result<?> c(@PathVariable("formId") String paramString, @RequestBody OnlCgformEnhanceJava paramOnlCgformEnhanceJava) {
        try {
            if (!org.boyan.modules.cgform.util.b.a(paramOnlCgformEnhanceJava))
                return Result.error("类实例化失败，请检查!");
            paramOnlCgformEnhanceJava.setCgformHeadId(paramString);
            if (this.onlCgformEnhanceService.checkOnlyEnhance(paramOnlCgformEnhanceJava)) {
                this.onlCgformEnhanceService.updateEnhanceJava(paramOnlCgformEnhanceJava);
                return Result.ok("保存成功!");
            }
            return Result.error("保存失败,配置重复了!");
        } catch (Exception exception) {
            a.error(exception.getMessage(), exception);
            return Result.error("保存失败!");
        }
    }

    @DeleteMapping({"/enhanceJava"})
    @CacheEvict(value = {"sys:cache:online:list", "sys:cache:online:form"}, allEntries = true, beforeInvocation = true)
    public Result<?> g(@RequestParam(name = "id", required = true) String paramString) {
        try {
            this.onlCgformEnhanceService.deleteEnhanceJava(paramString);
            return Result.ok("删除成功");
        } catch (Exception exception) {
            a.error(exception.getMessage(), exception);
            return Result.error("删除失败!");
        }
    }

    @DeleteMapping({"/deleteBatchEnhanceJava"})
    @CacheEvict(value = {"sys:cache:online:list", "sys:cache:online:form"}, allEntries = true, beforeInvocation = true)
    public Result<?> h(@RequestParam(name = "ids", required = true) String paramString) {
        try {
            List<String> list = Arrays.asList(paramString.split(","));
            this.onlCgformEnhanceService.deleteBatchEnhanceJava(list);
            return Result.ok("删除成功");
        } catch (Exception exception) {
            a.error(exception.getMessage(), exception);
            return Result.error("删除失败!");
        }
    }

    @GetMapping({"/queryTables"})
    public Result<?> a(@RequestParam(name = "tableName", required = false) String paramString, @RequestParam(name = "pageNo", defaultValue = "1") Integer paramInteger1, @RequestParam(name = "pageSize", defaultValue = "10") Integer paramInteger2, HttpServletRequest paramHttpServletRequest) {
        String str = JwtUtil.getUserNameByToken(paramHttpServletRequest);
        if (!"admin".equals(str))
            return Result.error("noadminauth");
        List<String> list = new ArrayList<>();
        try {
            list = DbReadTableUtil.readAllTableNames();
        } catch (SQLException sQLException) {
            a.error(sQLException.getMessage(), sQLException);
            return Result.error("同步失败，未获取数据库表信息");
        }
        org.boyan.modules.cgform.util.b.b(list);
        list = org.boyan.modules.cgform.util.b.f(list);
        List<String> list1 = this.onlCgformHeadService.queryOnlinetables();
        b();
        list.removeAll(list1);
        ArrayList<HashMap<Object, Object>> arrayList = new ArrayList<>();
        for (String str1 : list) {
            if (l(str1))
                continue;
            HashMap<Object, Object> hashMap = new HashMap<>();
            hashMap.put("id", str1);
            arrayList.add(hashMap);
        }
        return Result.ok(arrayList);
    }

    @PostMapping({"/transTables/{tbnames}"})
    public Result<?> d(@PathVariable("tbnames") String paramString, HttpServletRequest paramHttpServletRequest) {
        String str = JwtUtil.getUserNameByToken(paramHttpServletRequest);
        if (!"admin".equals(str))
            return Result.error("noadminauth");
        if (oConvertUtils.isEmpty(paramString))
            return Result.error("未识别的表名信息");
        if (c != null && c.equals(paramString))
            return Result.error("不允许重复生成!");
        c = paramString;
        String[] arrayOfString = paramString.split(",");
        for (String s : arrayOfString) {
            if (oConvertUtils.isNotEmpty(s)) {
                int i = this.onlCgformHeadService.count(new LambdaQueryWrapper<OnlCgformHead>()
                        .eq(OnlCgformHead::getTableName, s));
                if (i <= 0) {
                    a.info("[IP] [online数据库导入表]   --表名：" + s);
                    this.onlCgformHeadService.saveDbTable2Online(s);
                }
            }
        }
        c = null;
        return Result.ok("同步完成!");
    }

    @GetMapping({"/rootFile"})
    public Result<?> a() {
        JSONArray jSONArray = new JSONArray();
        File[] arrayOfFile = File.listRoots();
        for (File file : arrayOfFile) {
            JSONObject jSONObject = new JSONObject();
            if (file.isDirectory()) {
                jSONObject.put("key", file.getAbsolutePath());
                jSONObject.put("title", file.getPath());
                jSONObject.put("opened", Boolean.FALSE);
                JSONObject jSONObject1 = new JSONObject();
                jSONObject1.put("icon", "custom");
                jSONObject.put("scopedSlots", jSONObject1);
                jSONObject.put("isLeaf", (file.listFiles() == null || (Objects.requireNonNull(file.listFiles())).length == 0));
            }
            jSONArray.add(jSONObject);
        }
        return Result.ok(jSONArray);
    }

    @GetMapping({"/fileTree"})
    public Result<?> i(@RequestParam(name = "parentPath", required = true) String paramString) {
        JSONArray jSONArray = new JSONArray();
        File file = new File(paramString);
        File[] arrayOfFile = file.listFiles();
        assert arrayOfFile != null;
        for (File file1 : arrayOfFile) {
            if (file1.isDirectory() && oConvertUtils.isNotEmpty(file1.getPath())) {
                JSONObject jSONObject1 = new JSONObject();
                System.out.println(file1.getPath());
                jSONObject1.put("key", file1.getAbsolutePath());
                jSONObject1.put("title", file1.getPath().substring(file1.getPath().lastIndexOf(File.separator) + 1));
                jSONObject1.put("isLeaf", (file1.listFiles() == null || (Objects.requireNonNull(file1.listFiles())).length == 0));
                jSONObject1.put("opened", Boolean.FALSE);
                JSONObject jSONObject2 = new JSONObject();
                jSONObject2.put("icon", "custom");
                jSONObject1.put("scopedSlots", jSONObject2);
                jSONArray.add(jSONObject1);
            }
        }
        return Result.ok(jSONArray);
    }

    @GetMapping({"/tableInfo"})
    public Result<?> j(@RequestParam(name = "code", required = true) String paramString) {
        OnlCgformHead onlCgformHead = (OnlCgformHead)this.onlCgformHeadService.getById(paramString);
        if (onlCgformHead == null)
            return Result.error("未找到对应实体");
        HashMap<Object, Object> hashMap = new HashMap<>();
        hashMap.put("main", onlCgformHead);
        if (onlCgformHead.getTableType() == 2) {
            String str = onlCgformHead.getSubTableStr();
            if (oConvertUtils.isNotEmpty(str)) {
                ArrayList<OnlCgformHead> arrayList = new ArrayList<>();
                String[] arrayOfString = str.split(",");
                for (String str1 : arrayOfString) {
                    LambdaQueryWrapper<OnlCgformHead> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                    lambdaQueryWrapper.eq(OnlCgformHead::getTableName, str1);
                    OnlCgformHead onlCgformHead1 = this.onlCgformHeadService.getOne(lambdaQueryWrapper);
                    arrayList.add(onlCgformHead1);
                }
                Collections.sort(arrayList, (param1OnlCgformHead1, param1OnlCgformHead2) -> {
                    Integer integer1 = param1OnlCgformHead1.getTabOrderNum();
                    if (integer1 == null)
                        integer1 = 0;
                    Integer integer2 = param1OnlCgformHead2.getTabOrderNum();
                    if (integer2 == null)
                        integer2 = 0;
                    return integer1.compareTo(integer2);
                });
                hashMap.put("sub", arrayList);
            }
        }
        Integer integer = onlCgformHead.getTableType();
        if ("Y".equals(onlCgformHead.getIsTree()))
            integer = 3;
        List<Map<String, Object>> list = CgformEnum.getJspModelList(integer);
        hashMap.put("jspModeList", list);
        hashMap.put("projectPath", DbReadTableUtil.getProjectPath());
        return Result.ok(hashMap);
    }

    @PostMapping({"/copyOnline"})
    public Result<?> k(@RequestParam(name = "code", required = true) String paramString) {
        try {
            OnlCgformHead onlCgformHead = (OnlCgformHead)this.onlCgformHeadService.getById(paramString);
            if (onlCgformHead == null)
                return Result.error("未找到对应实体");
            this.onlCgformHeadService.copyOnlineTableConfig(onlCgformHead);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return Result.ok();
    }

    private boolean l(String paramString) {
        for (String str : b) {
            if (paramString.startsWith(str) || paramString.startsWith(str.toUpperCase()))
                return true;
        }
        return false;
    }

    private void b() {
        if (b == null) {
            Resource resource = this.resourceLoader.getResource("classpath:jeecg" + File.separator + "jeecg_config.properties");
            InputStream inputStream = null;
            try {
                inputStream = resource.getInputStream();
                Properties properties = new Properties();
                properties.load(inputStream);
                String str = properties.getProperty("exclude_table");
                if (str != null)
                    b = Arrays.asList(str.split(","));
            } catch (IOException iOException) {
                iOException.printStackTrace();
            } finally {
                if (inputStream != null)
                    try {
                        inputStream.close();
                    } catch (IOException iOException) {
                        iOException.printStackTrace();
                    }
            }
        }
    }
}
